import numpy as np
from sklearn.datasets import load_sample_images
from skimage import io
import cv2
# Visualisation libraries
## Text
from colorama import Fore, Back, Style
from IPython.display import Image, display, Markdown, Latex
## matplotlib
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
plt.style.use('seaborn-whitegrid')
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (17, 6)
%matplotlib inline
## seaborn
import seaborn as sns
import warnings
warnings.filterwarnings("ignore")
We use the sklearn image dataset to demonstrate basic operations using Open CV.
Images = load_sample_images()
Names = [x.split("\\")[-1].replace('.jpg','').title() for x in Images['filenames']]
Images = Images['images']
def ImShow(Images, Names, Title = 'Images', axis_tight = False):
fig, ax = plt.subplots(1, 2 , figsize = (17, 6))
ax = ax.ravel()
font = FontProperties()
font.set_weight('bold')
for i in range(2):
_ = ax[i].imshow(Images[i])
if axis_tight:
_ = ax[i].axis('tight')
_ = ax[i].axis('off')
_ = ax[i].set_title(Names[i], fontproperties=font, fontsize = 16)
_ = fig.subplots_adjust(wspace= 0.01)
if Title:
_ = fig.suptitle(Title, fontproperties=font, fontsize = 18)
return fig, ax
# Image Show
_,_ = ImShow(Images, Names, Title = 'Original Images')
We can use split funtion to access various channels of an image.
B, G, R = cv2.split(Images[0])
Alternatively, we could use, for example, use Img[:,:,0], Img[:,:,1] and Img[:,:,2] to access the red, green and blue chanells of Img.
fig, ax = plt.subplots(1, 3 , figsize = (17, 3.5))
ax = ax.ravel()
font = FontProperties()
font.set_weight('bold')
_ = fig.suptitle(Names[0], fontproperties=font, fontsize = 18)
Channels = ['Red', 'Green', 'Blue']
for i in range(3):
img = 0*Images[0]
img[:,:,i] = Images[0][:,:,i]
_ = ax[i].imshow(img)
_ = ax[i].set_aspect('auto')
_ = ax[i].axis('off')
_ = ax[i].text(x = 0, y = 0, s = Channels[i], size=14,
color = 'Navy', bbox=dict(boxstyle="square", ec='Navy', fc='LightSkyBlue'))
cv2.BORDER_CONSTANT: Adds a solid colored border.cv2.BORDER_REFLECT: Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcbcv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT: Border will be mirror reflection of the border elements, like this : like this : * gfedcb|abcdefgh|gfedcbacv2.BORDER_REPLICATE - Last element is replicated throughout, like this: aaaaaa|abcdefgh|hhhhhhhcv2.BORDER_WRAP - It will look like this : cdefgh|abcdefgh|abcdefgFor more information, please see the offical documentation here.
Dict = {cv2.BORDER_CONSTANT:'BORDER_CONSTANT',
cv2.BORDER_REPLICATE:'BORDER_REPLICATE',
cv2.BORDER_REFLECT:'BORDER_REFLECT',
cv2.BORDER_WRAP:'BORDER_WRAP',
cv2.BORDER_REFLECT_101:'BORDER_REFLECT_101'}
# RGB Colors
RGB = [[0,0,0] for i in range(3)]
for i in range(3):
RGB[i][i] += 255
fig, ax = plt.subplots(2, 3 , figsize = (17, 7))
ax = ax.ravel()
fig.delaxes(ax[-1])
font = FontProperties()
font.set_weight('bold')
_ = fig.suptitle(Names[1], fontproperties=font, fontsize = 18)
for i in range(len(Dict)):
img = cv2.copyMakeBorder(Images[1], 15, 15, 15, 15, i, value= RGB[0])
_ = ax[i].imshow(img)
_ = ax[i].set_aspect('auto')
_ = ax[i].axis('off')
_ = ax[i].set_title( Dict[i], fontproperties=font, fontsize = 12)
Image blending can be done using cv2.addWeighted. This function blends two images as follows
where $0\leq \alpha\leq 1$ and $\gamma$ is a an integer.
Augmented_Images = Images.copy()
# First Method
Augmented_Images[0] = cv2.addWeighted(Images[0],0.7, Images[1],0.3, 0)
# Second Method
Augmented_Images[1] = np.round(((0.7)*Images[0] + (0.3)*Images[1])).astype('uint8')
fig, ax = plt.subplots(1, 2 , figsize = (17, 6))
ax = ax.ravel()
font = FontProperties()
font.set_weight('bold')
_ = fig.suptitle('Image Blending', fontproperties=font, fontsize = 18)
Title = ['cv2.addWeighted(Images[0],0.7, Images[1],0.3, 0)', 'Our Function']
for i in range(2):
_ = ax[i].imshow(Augmented_Images[i])
_ = ax[i].set_aspect('auto')
_ = ax[i].axis('off')
_ = ax[i].set_title(Title[i], fontproperties=font, fontsize = 12)
This includes bitwise AND, OR, NOT and XOR operations.
Git = io.imread('https://github.githubassets.com/images/modules/open_graph/github-mark.png')
scale = 0.05 # of original size
Git_resized = cv2.resize(Git, tuple(np.dot(scale,Git.shape[:2][::-1]).astype(int)), interpolation = cv2.INTER_AREA)
## Invert
# Git_resized = 255 - Git_resized
Git_resized = cv2.bitwise_not(Git_resized)
Img = Images[0]
Mask = 0*Img
Mask[0: Git_resized.shape[0], 0:Git_resized.shape[1],:] = Git_resized
Img = cv2.add(Img,Mask)
fig, ax = plt.subplots(1, 2 , figsize = (17, 6))
_ = ax[0].imshow(Mask)
_ = ax[1].imshow(Img)
for i in range(len(ax)):
_ = ax[i].set_aspect('auto')
_ = ax[i].axis('off')
_ = ax[0].set_title('Logo', fontproperties=font, fontsize = 12)
_ = ax[1].set_title('Image + Logo', fontproperties=font, fontsize = 12)
Augmented_Images = Images.copy()
for i in range(len(Augmented_Images)):
Augmented_Images[i] = cv2.cvtColor(Augmented_Images[i],cv2.COLOR_BGR2GRAY)
fig, ax = plt.subplots(1, 2 , figsize = (17, 6))
ax = ax.ravel()
font = FontProperties()
font.set_weight('bold')
for i in range(2):
_ = ax[i].imshow(Augmented_Images[i],'gray')
_ = ax[i].axis('tight')
_ = ax[i].axis('off')
_ = ax[i].set_title(Names[i], fontproperties=font, fontsize = 16)
_ = fig.subplots_adjust(wspace= 0.01)
_ = fig.suptitle('RGB to Grayscale', fontproperties=font, fontsize = 18)
Augmented_Images = Images.copy()
for i in range(len(Augmented_Images)):
Augmented_Images[i] = cv2.bitwise_not(cv2.cvtColor(Augmented_Images[i],cv2.COLOR_BGR2GRAY))
fig, ax = plt.subplots(1, 2 , figsize = (17, 6))
ax = ax.ravel()
font = FontProperties()
font.set_weight('bold')
for i in range(2):
_ = ax[i].imshow(Augmented_Images[i],'gray')
_ = ax[i].axis('tight')
_ = ax[i].axis('off')
_ = ax[i].set_title(Names[i], fontproperties=font, fontsize = 16)
_ = fig.subplots_adjust(wspace= 0.01)
_ = fig.suptitle('RGB to Grayscale (Inverted)', fontproperties=font, fontsize = 18)
Augmented_Images = Images.copy()
for i in range(len(Augmented_Images)):
Augmented_Images[i] = cv2.cvtColor(Augmented_Images[i],cv2.COLOR_BGR2HSV)
_,_ = ImShow(Augmented_Images, Names, Title = 'RGB to HSV')